#!/bin/sh

# 生成一个用于输出目录根目录的小Makefile，以允许make从那里启动
# Makefile还允许更令人信服的外部模块构建

# 用法
# $1 - 源码目录
# $2 - 输出目录
# $3 - 版本
# $4 - 补丁级别


test ! -r $2/Makefile -o -O $2/Makefile || exit 0
# 仅覆盖自动生成的Makefiles
# (因此我们不覆盖内核Makefile)
if test -e $2/Makefile && ! grep -q Automatically $2/Makefile
then
	exit 0
fi
if [ "${quiet}" != "silent_" ]; then
	echo "  GEN     $2/Makefile"
fi

cat << EOF > $2/Makefile
# Automatically generated by $0: don't edit

VERSION = $3
PATCHLEVEL = $4

lastword = \$(word \$(words \$(1)),\$(1))
makedir := \$(dir \$(call lastword,\$(MAKEFILE_LIST)))

MAKEARGS := -C $1
MAKEARGS += O=\$(if \$(patsubst /%,,\$(makedir)),\$(CURDIR)/)\$(patsubst %/,%,\$(makedir))

MAKEFLAGS += --no-print-directory

.PHONY: all \$(MAKECMDGOALS)

all	:= \$(filter-out all Makefile,\$(MAKECMDGOALS))

all:
	\$(MAKE) \$(MAKEARGS) \$(all)

Makefile:;

\$(all): all
	@:

%/: all
	@:
EOF
